﻿

''' <summary>
''' 项目属性--应用程序--中的起动窗体改成fmuhook--来起动这个窗体。
''' </summary>
Public Class FmUhook
    Dim WithEvents myFmHook As uHook.FormHook
    Dim WithEvents MyKeyBoardHook As uHook.KeyboardHook
    Dim WithEvents MyMouseHook As uHook.MouseHook


    Private Sub FmUhook_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
        Call btnStop_Click(Nothing, Nothing)
    End Sub

    Private Sub FmUhook_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try

            MyKeyBoardHook = New uHook.KeyboardHook(False)
            MyMouseHook = New uHook.MouseHook(False)
            '  Me.WebBrowser1.Navigate("c:\头像.png")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
        End Try
    End Sub

    Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
        Try
            If MyKeyBoardHook IsNot Nothing Then MyKeyBoardHook.Stop()
            If MyMouseHook IsNot Nothing Then MyMouseHook.Stop()
            If myFmHook IsNot Nothing Then myFmHook.Stop()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
        End Try
    End Sub

    Private Sub LogWrite(ByVal txt As String)

        txtLog.AppendText(txt + Environment.NewLine)
        txtLog.SelectionStart = txtLog.Text.Length
    End Sub

    Private Sub MyKeyboardHook_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyKeyBoardHook.KeyDown
        'If e.KeyData.ToString = "A" Then
        '    End
        'End If

        LogWrite("KeyDown 	- " + e.KeyData.ToString())
    End Sub

    Private Sub MyKeyboardHook_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyKeyBoardHook.KeyPress
        LogWrite("KeyPress 	- " + e.KeyChar)
    End Sub

    Private Sub MyKeyboardHook_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyKeyBoardHook.KeyUp
        LogWrite("KeyUp 		- " + e.KeyData.ToString())
    End Sub

    Private Sub MyMouseHook_OnMouseActivity(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyMouseHook.OnMouseActivity
        Me.Left = e.X + 10
        Me.Top = e.Y + 10

        lblMousePosition.Text = String.Format("x={0}  y={1} wheel={2}", e.X, e.Y, e.Delta)
        If (e.Clicks > 0) Then LogWrite("MouseButton 	- " + e.Button.ToString())
    End Sub

    Private Sub FmUhook_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        '要在窗体上设置，这里设置会闪烁。

        'Me.ShowIcon = False '不显示图标
        'Me.ShowInTaskbar = False '不在任务栏显示图标。
        '将窗体的颜色设置为白色。
        ' Me.BackColor = Color.White
        '设置窗体的边框为无,因为边框不能透明。
        ' Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
        '过滤白色，窗体就变成了透明了。
        Me.TransparencyKey = Color.White  'BackColor
        Me.TopMost = True
        myFmHook = New uHook.FormHook
    End Sub


End Class